#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
户部尚赢期货量化交易系统 - 启动脚本
"""

import sys
import os
import logging

# 添加当前目录到Python路径
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler('system.log', encoding='utf-8'),
        logging.StreamHandler(sys.stdout)
    ]
)
logger = logging.getLogger(__name__)

def check_dependencies():
    """检查依赖项"""
    missing_modules = []
    
    # 检查核心模块
    try:
        import tkinter as tk
        from tkinter import ttk, messagebox
    except ImportError as e:
        missing_modules.append(f"tkinter: {e}")
    
    # 检查主要功能模块
    try:
        import main
        logger.info("main.py模块可用")
    except ImportError as e:
        logger.warning(f"main.py模块不可用: {e}")
        missing_modules.append(f"main.py: {e}")
    
    return missing_modules

def main():
    """主函数"""
    print("启动户部尚赢期货量化交易系统")
    print("=" * 50)
    
    # 检查依赖项
    print("检查系统依赖...")
    missing_modules = check_dependencies()
    
    if missing_modules:
        error_msg = "以下模块缺失或不可用：\n" + "\n".join(missing_modules)
        logger.error(f"依赖检查失败: {error_msg}")
        print(f"错误: {error_msg}")
        return
    
    print("所有依赖项检查通过")
    
    # 启动GUI界面
    try:
        from 户部尚赢期货量化交易系统 import MainApplication
        print("初始化GUI界面...")
        app = MainApplication()
        print("GUI界面启动成功")
        print("=" * 50)
        app.run()
        
    except Exception as e:
        error_msg = f"GUI启动失败: {e}"
        logger.error(error_msg)
        print(f"错误: {error_msg}")
        
    finally:
        print("GUI界面已关闭")

if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        print("\n用户中断，程序退出")
    except Exception as e:
        logger.error(f"程序异常退出: {e}")
        print(f"程序异常退出: {e}")
    finally:
        print("程序结束")